package com.xxx.mall.api;

import com.xxx.mall.bo.AuthAccountBO;
import com.xxx.mall.context.AuthUserContext;
import com.xxx.mall.dto.AuthenticationDTO;
import com.xxx.mall.manager.TokenStore;
import com.xxx.mall.response.R;
import com.xxx.mall.service.AuthAccountService;
import com.xxx.mall.vo.TokenInfoVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 统一账户信息 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-15
 */
@RestController
public class LoginApi {

    @Resource
    AuthAccountService authAccountService;
    @Resource
    TokenStore tokenStore;

    @PostMapping("/ua/login")
    public R<TokenInfoVO> login(@RequestBody @Valid AuthenticationDTO authenticationDTO) {
        //登录校验
        AuthAccountBO account = authAccountService.getInfoByUserNameAndPassword(authenticationDTO.getPrincipal(), authenticationDTO.getCredentials(), authenticationDTO.getSysType());
        //设置token信息,根据token保存用户信息
        return R.ok(tokenStore.storeAndGetVo(account));
    }

    @GetMapping("/test")
    public R<AuthAccountBO> xxx() {
        return R.ok(AuthUserContext.get());
    }
}
